package com.itheima.controller;

import com.itheima.domian.Book;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@RequestMapping("/book")
@Controller
public class BookController {
    @Autowired
    private BookService bookService;


    @RequestMapping("/addBook")
    public String addBookPage(){//跳转新增书籍的视图
        return "/admin/addBook";
    }

    @PostMapping("/addBooks")
    public String addBooks(Book books){
        System.out.println("addBooks==>"+books);
        bookService.addBook(books);
        return "redirect:/book/allBook";//直接重定向上面的处理方法，实现了请求复用
    }



    @RequestMapping("/allBook")
    public String allBook(Model model){
        List<Book> books = bookService.FindAll();
        model.addAttribute("book",books);
        return "/admin/book";
    }
    @RequestMapping("/addBooks")
    public String addBook(Book books){
        System.out.println("addBooks==>"+books);
        bookService.addBook(books);
        return "redirect:/book/allBook";//直接重定向上面的处理方法，实现了请求复用
    }


    @RequestMapping("/updateBook")
    public String updateBookPage(int id,Model model ){//跳转新增书籍的视图
        Book books = bookService.queryBookById(id);
        model.addAttribute("book",books);
        return "/admin/updateBook";
    }
    @RequestMapping("/updateBooks")
    public String updateBook(Book books){
        System.out.println("updateBook==>"+books);
       bookService.UpdataBook(books);
        System.out.println(1111);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/deleteBook")
    public String deleteBook(int id){
        System.out.println("deleteBook==>接收的ID = "+id);
        bookService.deleteBook(id);
        return "redirect:/book/allBook";
    }

//@RequestMapping("/deleteBook")
//public String deleteBook(int id){
//    System.out.println("deleteBook==>接收的ID = "+id);
//    bookService.deleteBookById(id);
//    return "redirect:/book/allBook";
//}

}
